package com.cunshe.web.manage;

import com.cunshe.common.DictConsts;
import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.domain.Menu;
import com.cunshe.entity.vo.MenuInsertVO;
import com.cunshe.entity.vo.MenuUpdateVO;
import com.cunshe.service.MenuService;
import com.cunshe.web.BaseController;
import com.sunkee.commons.utils.ConvertUtil;
import com.sunkee.commons.utils.ListUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 * @author xiao
 */
@Api(tags = "菜单")
@Login
@RestController
@RequestMapping(value = "/manage/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /** 新增 */
    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody @Validated MenuInsertVO vo) {
        Menu record = ConvertUtil.copyProperties(vo, Menu.class);
        record.setUpdateBy(getCurrentUser().getUsername());
        menuService.insert(record);
        return ResponseVO.success();
    }

    /** 修改 */
    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody @Validated MenuUpdateVO vo) {
        Menu record = ConvertUtil.copyProperties(vo, Menu.class);
        record.setUpdateBy(getCurrentUser().getUsername());
        menuService.update(record);
        return ResponseVO.success();
    }

    /** 删除 */
    @ApiOperation(value = "delete")
    @PostMapping(value = "delete")
    public ResponseVO delete(@RequestParam("id") Long id) {
        menuService.delete(id);
        return ResponseVO.success();
    }

    /** 查询菜单 */
    @ApiOperation(value = "查询菜单")
    @PostMapping(value = "tree")
    public ResponseVO tree() {
        List<Menu> list = menuService.list(null);
        list = (List<Menu>) ListUtils.list2Tree(list);
        return ResponseVO.success(list);
    }

}